package com.cbt.visitor.controller;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.cbt.business.po.*;
import com.cbt.business.service.*;
import org.aspectj.weaver.NewFieldTypeMunger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cbt.business.service.impl.CheckInRecordInfoServiceImpl;
import com.cbt.business.service.impl.ConfirmRecipientRecordInfoServiceImpl;
import com.cbt.business.service.impl.CopackRecordInfoServiceImpl;
import com.cbt.business.service.impl.DeliverRecordInfoServiceImpl;
import com.cbt.business.service.impl.FertilizerRecordInfoServiceImpl;
import com.cbt.business.service.impl.PestRecordInfoServiceImpl;
import com.cbt.business.service.impl.PickRecordInfoServiceImpl;
import com.cbt.visitor.po.ProductStatusInfo;
import com.cbt.visitor.service.ProductStatusInfoService;
import com.sun.org.apache.bcel.internal.generic.NEW;

@Controller
@RequestMapping("/visitor/")
public class VisitorQueryController 
{
	
	@Resource(name="sowRecordInfoServiceImpl")
	private SowRecordInfoService sowRecordInfoService;
	@Resource(name="fertilizerRecordInfoServiceImpl")
	private FertilizerRecordInfoService fertilizerRecordInfoService;
	@Resource(name="pestRecordInfoServiceImpl")
	private PestRecordInfoService pestRecordInfoService;
	@Resource(name="pickRecordInfoServiceImpl")
	private PickRecordInfoService pickRecordInfoService;
	@Resource(name="checkInRecordInfoServiceImpl")
	private CheckInRecordInfoService checkInRecordInfoService;
	@Resource(name="copackRecordInfoServiceImpl")
	private CopackRecordInfoService copackRecordInfoService;
	@Resource(name="deliverRecordInfoServiceImpl")
	private DeliverRecordInfoService deliverRecordInfoService;
	@Resource(name="confirmRecipientRecordInfoServiceImpl")
	private ConfirmRecipientRecordInfoService confirmRecipientRecordInfoService;
	@Resource(name="productStatusInfoServiceImpl")
	private ProductStatusInfoService productStatusInfoService;
	@Resource(name="businessInfoServiceImpl")
	private BusinessInfoService businessInfoService;
	
	@RequestMapping(value = "getWholeInfo.do")
	public @ResponseBody Map<String, Object> getWholeInfo(String queryId) throws Exception
	{
		System.out.println(queryId);
		if(queryId.length()!=25)
			return null;
		Map<String, Object> model =new HashMap<>();
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		
		//根据queryId的前13位可以获取sowrecord包装记录
		SowRecordInfo sowRecordInfo = sowRecordInfoService.getSowRecordInfoBySowSeg_btCode(queryId.substring(0, 13));
		if(sowRecordInfo!=null)
		{
			Map<String,Object> map = new HashMap<String,Object>();
			System.out.println("sow:		"+sowRecordInfo.toString());
//			model.addAttribute("sowRecord", sowRecordInfo);
			String sowTime=(sowRecordInfo.getSowtime());
			//时间单独处理
			String sowName = "产品记录";
			map.put(new String("时间".getBytes(),"utf-8"), sowTime);
			map.put(new String("环节".getBytes(),"utf-8"), new String(sowName.getBytes(),"utf-8"));
			map.put(new String("质量要求".getBytes(),"utf-8"), sowRecordInfo.getSeedsource());
			map.put(new String("生产地点".getBytes(),"utf-8"), sowRecordInfo.getSowlocation());
			map.put(new String("记录人员".getBytes(),"utf-8"), sowRecordInfo.getGrower());
			list.add(map);
			//map.clear();
		}
		//根据queryId的前13位可以获取fertilizerRecord包装记录
		List<FertilizerRecordInfo> fertilizerRecordInfos=fertilizerRecordInfoService.queryFertilizerByBtCodeService(queryId.substring(0, 13));
		if(fertilizerRecordInfos!=null){
			Map<String, Object> fertilizerMap=new HashMap<String, Object>();
			for(int i=0;i<fertilizerRecordInfos.size();i++){
				fertilizerMap.put(new String("时间".getBytes(),"utf-8"),fertilizerRecordInfos.get(i).getFertilizertime());
				fertilizerMap.put(new String("环节".getBytes(),"utf-8"), new String("生产".getBytes(),"utf-8"));
				fertilizerMap.put(new String("地点".getBytes(),"utf-8"), fertilizerRecordInfos.get(i).getFertilizername());
				fertilizerMap.put(new String("数量".getBytes(),"utf-8"), fertilizerRecordInfos.get(i).getFertilizernum());
				//fertilizerMap.put("FertilizerFrequency", fertilizerRecordInfos.get(i).getFertilizerfrequency());
				fertilizerMap.put(new String("生产人员".getBytes(),"utf-8"), fertilizerRecordInfos.get(i).getGrower());
				list.add(fertilizerMap);
			}
		}
		//根据queryId的前13位获取pestRecord病虫害记录
		List<PestRecordInfo> pestRecordInfos=pestRecordInfoService.queryPestByBtCodeService(queryId.substring(0,13));
		if(pestRecordInfos!=null){
			Map<String, Object> pestMap=new HashMap<String, Object>();
			for(int i=0;i<pestRecordInfos.size();i++){
				pestMap.put(new String("时间".getBytes(),"utf-8"),pestRecordInfos.get(i).getSprayTime());
				pestMap.put(new String("环节".getBytes(),"utf-8"), new String("原料".getBytes(),"utf-8"));
				pestMap.put(new String("类型".getBytes(),"utf-8"), pestRecordInfos.get(i).getPesttype());
				pestMap.put(new String("来源".getBytes(),"utf-8"), pestRecordInfos.get(i).getMedicationname());
				pestMap.put(new String("交接人员".getBytes(),"utf-8"), pestRecordInfos.get(i).getMedicationratio());
				pestMap.put(new String("数量".getBytes(),"utf-8"), pestRecordInfos.get(i).getMedicationdosage());
				pestMap.put(new String("运输方式".getBytes(),"utf-8"), pestRecordInfos.get(i).getResult());
				pestMap.put(new String("负责人员".getBytes(),"utf-8"), pestRecordInfos.get(i).getGrower());
				list.add(pestMap);
			}
		}
		//根据queryId的前17位获取采摘记录
		PickRecordInfo pickRecordInfos=pickRecordInfoService.queryPickByBtCode(queryId.substring(0,17));
		if(pickRecordInfos!=null){
			Map<String, Object> pickMap=new HashMap<String, Object>();
			pickMap.put(new String("时间".getBytes(),"utf-8"), pickRecordInfos.getPicktime());
			pickMap.put(new String("环节".getBytes(),"utf-8"), new String("质检".getBytes(),"utf-8"));
			pickMap.put(new String("方式".getBytes(),"utf-8"), pickRecordInfos.getPicktype());
			pickMap.put(new String("数量".getBytes(),"utf-8"), pickRecordInfos.getPicknum());
			pickMap.put(new String("人员".getBytes(),"utf-8"), pickRecordInfos.getGrower());
			list.add(pickMap);
		}
		//根据queryId的前21位获取检验入库信息
		CheckInRecordInfo checkInRecordInfo=checkInRecordInfoService.queryCheckInByBtCodeService(queryId.substring(0,21));
		if(checkInRecordInfo!=null){
			Map<String, Object> checkInMap=new HashMap<String, Object>();
			checkInMap.put(new String("时间".getBytes(),"utf-8"), checkInRecordInfo.getCheckintime());
			checkInMap.put(new String("环节".getBytes(),"utf-8"), new String("入库".getBytes(),"utf-8"));
			checkInMap.put(new String("存储地点".getBytes(),"utf-8"), checkInRecordInfo.getStocklocation());
			checkInMap.put(new String("库管人员".getBytes(),"utf-8"), checkInRecordInfo.getChecker());
			list.add(checkInMap);
		}
		//根据queryId25位数据获取包装信息
		CopackRecordInfo copackRecordInfo=copackRecordInfoService.queryCopackByBtCodeService(queryId);
		if(copackRecordInfo!=null){
			Map<String, Object> copackMap=new HashMap<String, Object>();
			copackMap.put(new String("时间".getBytes(),"utf-8"), copackRecordInfo.getCopacktime());
			copackMap.put(new String("环节".getBytes(),"utf-8"), new String("包装".getBytes(),"utf-8"));
			copackMap.put(new String("包装地点".getBytes(),"utf-8"), copackRecordInfo.getProducelocation());
			copackMap.put(new String("包装人员".getBytes(),"utf-8"), copackRecordInfo.getCopacker());
			//copackMap.put("product_btCode", copackRecordInfo.getProductBtcode());
			list.add(copackMap);
		}
		//根据包装信息里得到的物流单信息查询deliverRecord得到物流记录
		DeliverRecordInfo deliverRecordInfo=deliverRecordInfoService.queryDeliverByBtCodeService(queryId);
		if(deliverRecordInfo!=null){
			Map<String, Object> deliverMap=new HashMap<String,Object>();
			deliverMap.put(new String("时间".getBytes(),"utf-8"), deliverRecordInfo.getDelivertime());
			deliverMap.put(new String("环节".getBytes(),"utf-8"), new String("物流".getBytes(),"utf-8"));
			deliverMap.put(new String("运输起点".getBytes(),"utf-8"), deliverRecordInfo.getDeliveraddress());
			deliverMap.put(new String("运输部门".getBytes(),"utf-8"), deliverRecordInfo.getLogisticscompany());
			deliverMap.put(new String("分销人员".getBytes(),"utf-8"), deliverRecordInfo.getDistributor());
			//deliverMap.put("Logistics", deliverRecordInfo.getLogistics());
			list.add(deliverMap);
		}
		//根据包装信息里得到的物流单信息查询confirmrecipientRecord得到确认收货记录
		ConfirmRecipientRecordInfo confirmRecipientRecordInfo=confirmRecipientRecordInfoService.queryConfirmByOrderNumService(queryId);
		if(confirmRecipientRecordInfo!=null){
			Map<String, Object> confirmMap=new HashMap<String,Object>();
			
			confirmMap.put(new String("时间".getBytes(),"utf-8"), confirmRecipientRecordInfo.getRecipienttime());
			confirmMap.put(new String("环节".getBytes(),"utf-8"), new String("确认记录".getBytes(),"utf-8"));
			confirmMap.put(new String("物流单号".getBytes(),"utf-8"), confirmRecipientRecordInfo.getLogisticsnum());
			confirmMap.put(new String("人员".getBytes(),"utf-8"), confirmRecipientRecordInfo.getDistributor());
			list.add(confirmMap);
		}
		//更新查询次数，+1
		ProductStatusInfo productStatusInfo=new ProductStatusInfo();
		productStatusInfo.setProduct_btCode(queryId);
		productStatusInfo.setQueryTimes(productStatusInfoService.queryTimesByBtCodeService(queryId)+1);
		if(productStatusInfoService.updateQueryTimesByBtCodeService(productStatusInfo)>0){
			System.out.println("更新查询次数成功");
		}else{
			System.out.println("queryTimes Fail");
		}
		String s = queryId.substring(0, 3);
		System.out.println(s);
		BusinessInfo businessInfo=new BusinessInfo();
		businessInfo.setBusinessId(s);
		BusinessInfo info = businessInfoService.getBusinessInfo(businessInfo);
		model.put("Info", list);
		model.put("com",info.getBusinessName());
		return model;
	}
	
	
	/*
	 *首页搜索
	 */
	@RequestMapping("checkProductBtcode.do")
	public @ResponseBody String checkProductBtcode(String productBtcode)
	{
		ProductStatusInfo productStatusInfo=new ProductStatusInfo();		
		return productStatusInfoService.checkProductBtcode(productBtcode).toString();
	}
	
	@RequestMapping("getData.do")
	public @ResponseBody ModelMap getData(String businessId)
	{
		ModelMap model = new ModelMap();
		HashMap<String,Integer> map = productStatusInfoService.getData(businessId);
		model.addAttribute("map", map);
		return model;
	}
	
}
